#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

typedef struct COPM
{
	bool operator()(pair<string, int>& a, pair<string, int>& b)
	{
		return (a.second > b.second) || (a.second == b.second && a.first < b.first);
	}
}COMP;
class Solution {
public:

	vector<string> topKFrequent(vector<string>& words, int k)
	{
		map<string, int> mp;

		for (int i = 0; i < words.size(); i++)
		{
			mp[words[i]]++;
		}

		vector<pair<string, int>> sv(mp.begin(), mp.end());
		sort(sv.begin(), sv.end(), COMP());

		vector<string> res;
		for (auto it : sv)
		{
			res.push_back(it.first);
		}

		return res;
	}
};